//
//  Networkable.swift
//  LiveShow
//
//  Created by dai zhong on 2017/5/10.
//  Copyright © 2017年 company.cn. All rights reserved.
//

import Foundation
import Alamofire

enum RequestType {
    case get
    case post
}

protocol NetworkProtocol {
    
    var requestType :RequestType { get }
    var urlString :String { get }
    var parameters:[String :Any]? { get }
    
    func parseData(_ result :Any)
}

extension NetworkProtocol{

    func requestData(_ finished:@escaping ()->()){
        
        
        let method = requestType == RequestType.get ? HTTPMethod.get :HTTPMethod.post;
        Alamofire.request(urlString, method: method, parameters: parameters).responseJSON { (dataResponse:DataResponse<Any>) in
            
            
            if let error = dataResponse.result.error{
                print("网络请求有错误:", error)
                return
            }

            guard let result = dataResponse.result.value else{
                
                return;
            }
            
            self.parseData(result)
            
            finished()
        }
    }
    
    func addCommonParams() -> Dictionary<String, Any>? {
    
        return nil
    }
}



































